package pers.xiaojun.boot.module.system.controller.dict;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import pers.xiaojun.boot.mybatis.core.convert.PageConvert;
import pers.xiaojun.boot.common.pojo.CommonResult;
import pers.xiaojun.boot.common.pojo.PageResult;
import pers.xiaojun.boot.common.util.BeanUtils;
import pers.xiaojun.boot.module.system.controller.dict.vo.type.DictTypeQueryReqVO;
import pers.xiaojun.boot.module.system.controller.dict.vo.type.DictTypeRespVO;
import pers.xiaojun.boot.module.system.controller.dict.vo.type.DictTypeSaveReqVO;
import pers.xiaojun.boot.module.system.controller.dict.vo.type.DictTypeSimpleRespVO;
import pers.xiaojun.boot.module.system.service.dict.DictTypeService;
import pers.xiaojun.boot.web.core.annotation.AdminApi;

import java.util.List;


/**
 * 字典接口
 *
 * @author xiaojun
 * @since 2025-11-05
 */
@Tag(name = "字典")
@AdminApi
@RestController
@RequestMapping("/system/dict-type")
@RequiredArgsConstructor
public class DictTypeController {

    private final DictTypeService dictTypeService;

    @PostMapping
    @Operation(summary = "创建字典")
    @PreAuthorize("@ss.hasPermission('system:dict:create')")
    public CommonResult<?> createDateType(@Valid @RequestBody DictTypeSaveReqVO createVO) {
        return CommonResult.success(dictTypeService.saveDictType(createVO).getId());
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新字典")
    @PreAuthorize("@ss.hasPermission('system:dict:update')")
    @Parameter(name = "id", description = "字典Id", required = true)
    public CommonResult<?> updateDateType(@PathVariable("id") Long deptId, @Valid @RequestBody DictTypeSaveReqVO updateVO) {
        updateVO.setId(deptId);
        return CommonResult.success(dictTypeService.updateDictType(updateVO).getId());
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除字典")
    @PreAuthorize("@ss.hasPermission('system:dict:delete')")
    @Parameter(name = "id", description = "字典Id", required = true)
    public CommonResult<?> deleteDateType(@PathVariable("id") Long deptId) {
        dictTypeService.removeDictTypeById(deptId);
        return CommonResult.success();
    }

    @DeleteMapping("/batch/{ids}")
    @Operation(summary = "删除一批字典")
    @PreAuthorize("@ss.hasPermission('system:dict:delete')")
    @Parameter(name = "ids", description = "字典Id列表", required = true)
    public CommonResult<?> deleteDateType(@PathVariable("ids") List<Long> deptIds) {
        dictTypeService.removeDictTypeByIds(deptIds);
        return CommonResult.success();
    }

    @GetMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:dict:query')")
    @Operation(summary = "获取字典数据", description = "根据Id获取字典数据")
    @Parameter(name = "id", description = "字典Id", required = true)
    public CommonResult<DictTypeRespVO> getDictType(@PathVariable("id") Long deptId) {
        return CommonResult.success(BeanUtil.toBean(dictTypeService.getDeptTypeById(deptId), DictTypeRespVO.class));
    }

    @GetMapping("/simple-list")
    @PreAuthorize("@ss.hasPermission('system:dict:query')")
    @Operation(summary = "获取字典类型简要信息数据", description = "获取字典类型简要信息数据")
    public CommonResult<List<DictTypeSimpleRespVO>> getSimpleDictType() {
        return CommonResult.success(BeanUtils.toBeans(dictTypeService.list(), DictTypeSimpleRespVO.class));
    }

    @GetMapping("/page")
    @PreAuthorize("@ss.hasPermission('system:dict:query')")
    @Operation(summary = "获取字典类型分页数据", description = "获取字典类型分页（条件查询）数据")
    public CommonResult<PageResult<DictTypeRespVO>> getDictTypePage(@Valid DictTypeQueryReqVO queryVO) {
        return CommonResult.success(PageConvert.INSTANCE.toPageResult(dictTypeService.listDictTypesPage(queryVO), DictTypeRespVO.class));
    }

}
